/*
 * Copyright (C) 2024-2025 Deepin Technology Co., Ltd.
 *
 * Author:     zhangbingyi <zhangbingyi@uniontech.com>
 *
 * Maintainer: zhangbingyi <zhangbingyi@uniontech.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "netctrlmodel.h"
#include "common.h"
#include "compixmap.h"
#include "gsettingkey.h"
#include "invokers/invokerfactory.h"

#include <QDebug>

NetCtrlModel::NetCtrlModel(QObject *parent)
    : QObject(parent)
    , m_DataInterFaceServer(nullptr)
    , m_monitorInterFaceServer(nullptr)
    , m_gsetting(nullptr)
{
    // 初始化数据
    initData();

    // 初始化信号槽
    initSignalSLot();
}

NetCtrlModel::~NetCtrlModel()
{
}

// 异步获取联网管控应用信息
void NetCtrlModel::asyncGetNetAppInfoList()
{
    // 获取联网管控所有APP信息
    DBUS_NOBLOCK_INVOKE(m_DataInterFaceServer, "getNetAppsInfo");
}

// 改变联网管控应用状态
void NetCtrlModel::changeNetAppStatus(const QString &pkgName, int status)
{
    // 将包名以及对应的类型发送给服务
    DBUS_NOBLOCK_INVOKE(m_DataInterFaceServer, "setNetAppStatusChange", pkgName, status);
}

// 联网管控功能是否开启
bool NetCtrlModel::isNetCtrlOpen()
{
    return m_gsetting->GetValue(NET_CONTROL_ON_OFF).toBool();
}
// 添加日志
void NetCtrlModel::addSecurityLog(int type, const QString &log)
{
    DBUS_NOBLOCK_INVOKE(m_monitorInterFaceServer, "AddSecurityLog", type, log);
}

// 初始化数据
void NetCtrlModel::initData()
{
    // 注册
    registerDefenderProcInfoMetaType();
    registerDefenderProcInfoListMetaType();

    m_DataInterFaceServer = InvokerFactory::GetInstance().CreateInvoker("com.deepin.defender.datainterface", "/com/deepin/defender/datainterface", "com.deepin.defender.datainterface", ConnectType::SESSION, this);
    m_monitorInterFaceServer = InvokerFactory::GetInstance().CreateInvoker("com.deepin.defender.MonitorNetFlow", "/com/deepin/defender/MonitorNetFlow", "com.deepin.defender.MonitorNetFlow", ConnectType::SYSTEM, this);
    // 初始化安全中心gsetting配置
    m_gsetting = InvokerFactory::GetInstance().CreateSettings("com.deepin.dde.deepin-defender", QByteArray(), this);
}

// 初始化信号
void NetCtrlModel::initSignalSLot()
{
    // 连接信号槽
    m_DataInterFaceServer->Connect("requestNetAppsChange", this, SLOT(sendNetAppInfoList(DefenderProcInfoList)));
}
